#!/bin/bash
#
# $Id: post-mysqld-install 3661 2013-06-25 22:04:33Z dhill $
#
# Post-install steps for columnstore-mysql install

prefix=/usr/local
installdir=$prefix/mariadb/columnstore
rpmmode=install
user=mysql
tmpdir="/tmp"

password=" "
pwprompt=

if [ $EUID -ne 0 ]; then
	USER=`whoami 2>/dev/null`
	user=$USER
fi

for arg in "$@"; do
	if [ `expr -- "$arg" : '--prefix='` -eq 9 ]; then
		prefix="`echo $arg | awk -F= '{print $2}'`"
		installdir=$prefix/mariadb/columnstore
	elif [ `expr -- "$arg" : '--rpmmode='` -eq 10 ]; then
		rpmmode="`echo $arg | awk -F= '{print $2}'`"
	elif [ `expr -- "$arg" : '--installdir='` -eq 13 ]; then
		installdir="`echo $arg | awk -F= '{print $2}'`"
		prefix=`dirname $installdir`
	elif [ `expr -- "$arg" : '--user='` -eq 7 ]; then
		user="`echo $arg | awk -F= '{print $2}'`"
	elif [ $(expr -- "$arg" : '--password=') -eq 11 ]; then
		password="$(echo $arg | awk -F= '{print $2}')"
	elif [ $(expr -- "$arg" : '--tmpdir=') -eq 9 ]; then
		tmpdir="$(echo $arg | awk -F= '{print $2}')"
	else
		echo "post-mysqld-install: ignoring unknown argument: $arg" 1>&2
	fi
done

#cd $installdir/mysql/lib
#for file in libmysqlclient libmysqlclient_r; do
#	ln -sf $file.so.18.0.0 $file.so
#	ln -sf $file.so.18.0.0 $file.so.18
#done
cd $installdir/mysql/lib/plugin
for file in ha_archive ha_blackhole ha_federated ha_innodb; do
	ln -sf $file.so $file.so.0
done

### taken from MySQL-server-5.1.30-0.glibc23.i386.rpm
mysql_datadir=$installdir/mysql/db

# Create data directory if needed
if test ! -d $mysql_datadir; then mkdir -m 755 $mysql_datadir; fi
if test ! -d $mysql_datadir/mysql; then mkdir $mysql_datadir/mysql; fi
if test ! -d $mysql_datadir/test; then mkdir $mysql_datadir/test; fi
if test ! -d $installdir/mysql/lib; then mkdir $installdir/mysql/lib; fi
if test ! -d $installdir/mysql/lib/mysql; then mkdir $installdir/mysql/lib/mysql; fi

# Create a MySQL user and group. Do not report any problems if it already
# exists.
groupadd -r $user 2> /dev/null || true
useradd -m -r -d $mysql_datadir -s /bin/bash -c "MySQL server" -g $user $user 2> /dev/null || true
# The user may already exist, make sure it has the proper group nevertheless (BUG#12823)
usermod -g $user $user 2> /dev/null || true

# Change permissions so that the user that will run the MySQL daemon
# owns all database files.
chown -R $user:$user $installdir/mysql

# Initiate databases if needed
if [ $installdir != "/usr/local/mariadb/columnstore" ]; then
	sed -i -e s@/usr/local/mariadb/columnstore@$installdir@g $installdir/mysql/my.cnf
fi

# InfiniDB testing hook...
test -x /usr/local/bin/idb-testing-mysql-pre-start && /usr/local/bin/idb-testing-mysql-pre-start $installdir/mysql

# If DB exist, run upgrade script if it exist
if [ -d $installdir/mysql/db/calpontsys ]; then

	if [ -x $installdir/mysql/mysql-Columnstore ]; then
		# Restart in the same way that mysqld will be started normally.
		$installdir/mysql/mysql-Columnstore stop >/dev/null 2>&1
		sleep 2
		$installdir/mysql/mysql-Columnstore start

		sleep 5
		
		# Run MariaDB (mysql) upgrade script, if it exist
		if [ -x $installdir/mysql/bin/mysql_upgrade ]; then
			echo "Running mysql_upgrade script"
			if [[ ${password} == " " ]]; then
				$installdir/mysql/bin/mysql_upgrade --defaults-file=$installdir/mysql/my.cnf > $tmpdir/mysql_upgrade.log
				if [ $? -ne 0 ]; then
					echo "ERROR: mysql_upgrade failure, check $tmpdir/mysql_upgrade.log"
					$installdir/mysql/mysql-Columnstore stop
					sleep 2
					exit 2;
				fi
			else
				$installdir/mysql/bin/mysql_upgrade --defaults-file=$installdir/mysql/my.cnf --password=$password  > $tmpdir/mysql_upgrade.log
				if [ $? -ne 0 ]; then
					echo "ERROR: mysql_upgrade failure, check $tmpdir/mysql_upgrade.log"
					$installdir/mysql/mysql-Columnstore stop
					sleep 2
					exit 2;
				fi
			fi
		fi
		
		# stop
		$installdir/mysql/mysql-Columnstore stop
		sleep 2
	fi
fi

### Don't give the user the notes, we'll fix them ourselves...
$installdir/mysql/scripts/mysql_install_db --rpm --user=$user --defaults-extra-file=$installdir/mysql/my.cnf --basedir=$installdir/mysql >/dev/null
# Change permissions again to fix any new files.
chown -R $user:$user $mysql_datadir

# Fix permissions for the permission database so that only the user
# can read them.
chmod -R og-rw $mysql_datadir/mysql

# Change permissions again to fix any new files.
chown -R $user.$user $installdir/mysql

if [ -f $installdir/lib/libcalmysql.so.1.0.0 ]; then
	libcalmysql=$installdir/lib/libcalmysql.so.1.0.0
	libudfsdk=$installdir/lib/libudf_mysql.so.1.0.0
	libregrsdk=$installdir/lib/libregr_mysql.so.1.0.0
    is_columnstore_tables=$installdir/lib/is_columnstore_tables.so.1.0.0
    is_columnstore_columns=$installdir/lib/is_columnstore_columns.so.1.0.0
    is_columnstore_extents=$installdir/lib/is_columnstore_extents.so.1.0.0
    is_columnstore_files=$installdir/lib/is_columnstore_files.so.1.0.0
elif [ -f $installdir/lib/libcalmysql.so.1 ]; then
	libcalmysql=$installdir/lib/libcalmysql.so.1
	libudfsdk=$installdir/lib/libudf_mysql.so.1
	libregrsdk=$installdir/lib/libregr_mysql.so.1
    is_columnstore_tables=$installdir/lib/is_columnstore_tables.so.1
    is_columnstore_columns=$installdir/lib/is_columnstore_columns.so.1
    is_columnstore_extents=$installdir/lib/is_columnstore_extents.so.1
    is_columnstore_files=$installdir/lib/is_columnstore_files.so.1
else
	libcalmysql=
fi

if [ -n "$libcalmysql" ]; then

	cd $installdir/mysql/lib/plugin
	ln -sf $libcalmysql libcalmysql.so
    ln -sf $libcalmysql libcalmysqlent.so
	ln -sf $libudfsdk libudf_mysql.so
	ln -sf $libregrsdk libregr_mysql.so
    ln -sf $is_columnstore_tables is_columnstore_tables.so
    ln -sf $is_columnstore_columns is_columnstore_columns.so
    ln -sf $is_columnstore_extents is_columnstore_extents.so
    ln -sf $is_columnstore_files is_columnstore_files.so
fi

# cleanup previous install mysql replication files
rm -rf $mysql_datadir/*relay*
rm -rf $mysql_datadir/mysql-bin.*
rm -rf $mysql_datadir/master.info

exit 0
